package com.wuze.cloud.result;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

/**
 * @author: wuze
 * @description: 返回值包装类
 * @date: 2023/02/14
 */
@JsonInclude(Include.NON_NULL)
public class Result<T> {

    private int code;

    private String msg;

    private T data;

    public static <T> Result<T> success() {
        return new Result<T>();
    }

    public static <T> Result<T> success(T result) {
        Result<T> response = new Result<T>();
        response.setData(result);
        return response;
    }

    public static <T> Result<T> validfail(String msg) {
        Result<T> response = new Result<T>();
        response.setCode(500);
        response.setMsg(msg);
        return response;
    }

    public static <T> Result<T> validfail(Integer code, String msg) {
        Result<T> response = new Result<T>();
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }

    public Result() {
        this(0, "");
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @JsonIgnore
    public Boolean isSuccessful() {
        return this.code == 0;
    }

    @Override
    public String toString() {
        return "RestResponse [code=" + code + ", msg=" + msg + ", result="
                + data + "]";
    }

}

